//
//  FriendDataCenter.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/9.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "FriendDataCenter.h"
#import "FriendModel.h"
@implementation FriendDataCenter

- (void)fetchFriendListComplation:(void (^)(id, NSString *))complation
{
    UserModel *user = [UserManager fetchUserInfo];
    NSDictionary *param = @{@"user":[NSNumber numberWithInteger:user.userId]};
    
    [HttpRequest POSTRequest:FriendList WithParams:param WithSuccess:^(id responseOjbect) {

        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            NSArray *listData = (NSArray *)[responseOjbect objectForKey:@"data"];
            NSArray *modelData = [NSArray yy_modelArrayWithClass:[FriendModel class] json:listData];
            complation(modelData,nil);
        }else{
            complation(nil,[responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {

        complation(nil,@"服务器异常");
    }];
}

- (void)searchFriendWithPhone:(NSString *)phone complation:(void (^)(id, NSString *))complation
{
    NSDictionary *param = @{@"phone":phone};
    
    [HttpRequest POSTRequest:SearchFriend WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            
            UserModel *friendInfo = [UserModel yy_modelWithJSON:[responseOjbect objectForKey:@"data"]];
            complation(friendInfo,nil);
            
        }else{
            complation(nil,[responseOjbect objectForKey:@"message"]);
        }

    } WithFail:^(NSError *error) {
        complation(nil,@"服务器异常");
    }];
}

- (void)addFriendWithFriend:(NSString *)friendId complation:(void (^)(NSString *))complation
{
    UserModel *user = [UserManager fetchUserInfo];
    NSDictionary *param = @{
                            @"user":[NSNumber numberWithInteger:user.userId],
                            @"friend":friendId,
                            };
    [HttpRequest POSTRequest:AddFriend WithParams:param WithSuccess:^(id responseOjbect) {
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

- (void)remarkFriend:(FriendModel *)friendModel remark:(NSString *)remark complation:(void (^)(NSString *))complation
{
    FriendModel *friend = friendModel;
    NSDictionary *param = @{
                            @"user":[NSNumber numberWithInteger:friend.userId],
                            @"friend":[NSNumber numberWithInteger:friend.friendId],
                            @"remark":remark,
                            };
    [HttpRequest POSTRequest:ModifyRemark WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

- (void)deleteFriend:(FriendModel *)friendModel complation:(void (^)(NSString *))complation
{
    FriendModel *friend = friendModel;
    NSDictionary *param = @{
                            @"user":[NSNumber numberWithInteger:friend.userId],
                            @"friend":[NSNumber numberWithInteger:friend.friendId],
                            };
    [HttpRequest POSTRequest:DeleteFriend WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

@end
